import java.awt.Checkbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.*;

/**
 * @author Benjamin GODDE
 *
 */

@SuppressWarnings("serial")
class RechercheModele extends JFrame implements ActionListener, MouseListener, KeyListener
{

	private JPanel pane;
	private JLabel designation;
	private JLabel client;
	private JLabel numero;
	private JLabel numeroPlan;
	private JLabel annee;
	private Checkbox vieux;
	private JTextField clientTxt;
	private JTextField designationTxt;
	private JTextField numeroTxt;
	private JTextField numeroPlanTxt;
	private JTextField anneeTxt;
	private JButton okBut;
	private JButton visBut;
	//romeo
	private JButton PDFBut;
	//
	private JMenuBar menuBar;
	private JMenu fichier;
	private JMenu mModele;
	private JMenu mClient;
	private JMenuItem deconnexion;
	private JMenuItem miRechercherM;
	private JMenuItem miAjouterM;
	private JMenuItem miModifierM;
	private JMenuItem miRechercherC;
	private JMenuItem miAjouterC;
	private JMenuItem miModifierC;
	private JTable tableau;
	private UneditableTableModel tab;
	private JScrollPane scrollpane;
	private String query;
	private static String [] [] donnees;
	private String desiQ;
	private String numPlanQ;
	private String numModeleQ;
	private int ligneVisu;
	


	public String getQuery() {
		return this.query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public JMenuItem getMiModifierM() {
		return miModifierM;
	}

	public void setMiModifierM(JMenuItem miModifierM) {
		this.miModifierM = miModifierM;
	}

	public JMenuItem getMiRechercherC() {
		return miRechercherC;
	}

	public void setMiRechercherC(JMenuItem miRechercherC) {
		this.miRechercherC = miRechercherC;
	}

	public JMenuItem getMiModifierC() {
		return miModifierC;
	}

	public void setMiModifierC(JMenuItem miModifierC) {
		this.miModifierC = miModifierC;
	}

	public static String [] [] getDonnees() {
		return donnees;
	}

	public static void setDonnees(String [] [] donnees) {
		RechercheModele.donnees = donnees;
	}
	
	public String getDesiQ() {
		return desiQ;
	}

	public void setDesiQ(String desiQ) {
		this.desiQ = desiQ;
	}

	public String getNumPlanQ() {
		return numPlanQ;
	}

	public void setNumPlanQ(String numPlanQ) {
		this.numPlanQ = numPlanQ;
	}

	public String getNumModeleQ() {
		return numModeleQ;
	}

	public void setNumModeleQ(String numModeleQ) {
		this.numModeleQ = numModeleQ;
	}
	public RechercheModele()
	{
		super("Model Manager");
		this.setBounds(100,100,600,550 );

		this.menuBar = new JMenuBar();
		this.fichier = new JMenu("Fichier");
		this.mModele = new JMenu("Mod�le");
		this.mClient = new JMenu("Client");
		this.deconnexion = new JMenuItem("DŽconnexion");
		this.miRechercherM = new JMenuItem("Rechercher");
		this.miAjouterM = new JMenuItem("Ajouter");
		this.setMiModifierM(new JMenuItem("Modifier"));
		this.miAjouterC = new JMenuItem("Ajouter");

		this.menuBar.add(this.fichier);
		this.menuBar.add(this.mModele);
		this.menuBar.add(this.mClient);
		this.fichier.add(this.deconnexion);
		this.mModele.add(this.miRechercherM);
		this.mModele.add(this.miAjouterM);
		this.mClient.add(this.miAjouterC);
		this.setJMenuBar(this.menuBar);

		this.tableau = new JTable();
		this.pane = new JPanel();
		this.designation = new JLabel("Designation");
		this.client = new JLabel("Client");
		this.numero = new JLabel("N¡ Mod�le");
		this.numeroPlan = new JLabel("N¡ Plan");
		this.annee = new JLabel("AnnŽe Mod�le");
		this.clientTxt = new JTextField("");
		this.designationTxt = new JTextField("");
		this.numeroTxt = new JTextField("");
		this.numeroPlanTxt = new JTextField("");
		this.anneeTxt = new JTextField("");
		this.okBut = new JButton("Rechercher");
		//romeo
		this.PDFBut = new JButton("Générer PDF");
		//
		this.visBut = new JButton("Visualiser");
		this.vieux = new Checkbox("PŽrimŽ");
		this.setDesiQ(new String(""));
		this.setNumPlanQ(new String (""));
		this.setNumModeleQ(new String(""));
		this.pane.setBorder(BorderFactory.createTitledBorder("Recherche"));


		this.okBut.addActionListener(this);
		//romeo
		this.PDFBut.addActionListener(this);
		//
		this.miAjouterC.addActionListener(this);
		this.miRechercherM.addActionListener(this);
		this.miAjouterM.addActionListener(this);
		this.visBut.addActionListener(this);
		this.deconnexion.addActionListener(this);
		
		this.okBut.addKeyListener(this);
		this.clientTxt.addKeyListener(this);
		this.designationTxt.addKeyListener(this);
		this.numeroPlanTxt.addKeyListener(this);
		this.numeroTxt.addKeyListener(this);
		this.anneeTxt.addKeyListener(this);


		this.getContentPane().add(this.pane);
		this.pane.add(designation);
		this.pane.add(client);
		this.pane.add(numero);
		this.pane.add(numeroPlan);
		this.pane.add(annee);
		this.pane.add(designationTxt);
		this.pane.add(clientTxt);
		this.pane.add(numeroTxt);
		this.pane.add(numeroPlanTxt);
		this.pane.add(anneeTxt);
		this.pane.add(okBut);
		//romeo
		this.pane.add(PDFBut);
		//
		this.pane.add(vieux);
		this.pane.add(visBut);



		this.getContentPane().setLayout(null);
		this.pane.setLayout(null);

		this.pane.setBounds(5,5,590,490);
		this.designation.setBounds(30,20,120,20);
		this.client.setBounds(290,20,120,20);
		this.numero.setBounds(30,60,120,20);
		this.numeroPlan.setBounds(290,60,120,20);
		this.annee.setBounds(30,100,120,20);
		this.designationTxt.setBounds(150,20,120,20);
		this.numeroTxt.setBounds(150,60,120,20);
		this.numeroPlanTxt.setBounds(410,60,120,20);
		this.vieux.setBounds(290,100,120,20);
		this.clientTxt.setBounds(410,20,120,20);
		this.anneeTxt.setBounds(150,100,120,20);
		this.okBut.setBounds(155,140,110,20);
		//romeo
		this.PDFBut.setBounds(420,400,110,20);
		//
		this.visBut.setBounds(420,449, 110, 20 );


		this.scrollpane = new JScrollPane();
		getContentPane().add(this.scrollpane) ;
		this.pane.add(this.scrollpane);
		this.scrollpane.setBounds(30,180,380,290);


		this.setResizable(false);
		this.setVisible(true);
	}

	public void mouseClicked (MouseEvent e)
	{

		this.ligneVisu = tableau.getSelectedRow();
		tableau.setRowSelectionAllowed(true);
		
	}

	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	
	public void keyPressed(KeyEvent evt) {

		if ( evt.getKeyCode() == KeyEvent.VK_ENTER)
		{
			this.connec();
		}
	}
	public void keyTyped(KeyEvent evt) {}
	public void keyReleased(KeyEvent evt) {}
	
	public void connec(){
		int anneeModeleR;
		query = "";
		String designationR= designationTxt.getText();
		String clientR= clientTxt.getText();
		String numModeleR= numeroTxt.getText();
		String numPlanR= numeroPlanTxt.getText();
		query = "Select c.nom_client, m.designation_modele, m.numero_modele, m.numero_plan_modele, m.annee_modele from modele m join client c on m.id_client = c.id_client  where 1 ";

		if(anneeTxt.getText().length() != 0){
			anneeModeleR = Integer.parseInt(anneeTxt.getText());
			query += "and m.annee_modele = \"" + anneeModeleR + "\" " ;
		}
		if(designationTxt.getText().length() != 0){
			query += "and m.designation_modele LIKE \'" + designationR + "%\' ";
		}
		if(clientTxt.getText().length() != 0){
			query += "and c.nom_client LIKE \'" + clientR + "%\' ";
		}
		if(numeroTxt.getText().length() != 0){
			query += "and m.numero_modele LIKE \'" + numModeleR + "%\' ";
		}
		if(numeroPlanTxt.getText().length() != 0){
			query += "and m.numero_plan_modele LIKE \'" + numPlanR + "%\' ";
		}
		if(this.vieux.getState()){
			query += "and annee_modele < YEAR( SYSDATE() ) -5";
		}


		SelectData.REQUETE = query;
		try{

			ResultSet result = SelectData.startSelect();
			@SuppressWarnings("unused")
			ResultSetMetaData resultMeta = result.getMetaData();
			int nbLignes = 0;

			try{
				while(result.next()){
					nbLignes ++;
				}
			}catch(SQLException h){
				System.out.println(h); 
			}
			String [] titreColonnes = { "Nom Client","Designation","Numero Modele", "Numero Plan", "Annee"};
			setDonnees(new  String[nbLignes][5]);
			int i=0;
			SelectData.REQUETE = this.getQuery();
			ResultSet result2 = SelectData.startSelect();
			try{
				while(result2.next()){
					getDonnees() [i][0] = result2.getString("nom_client");
					getDonnees() [i][1] = result2.getString("designation_modele");
					getDonnees() [i][2] = result2.getString("numero_modele");
					getDonnees() [i][3] = result2.getString("numero_plan_modele");
					getDonnees() [i][4] = result2.getString("annee_modele");
					i++;
				}

			}catch(SQLException ex){
				System.out.println(""); 
			}

			this.tab = new UneditableTableModel(getDonnees(),titreColonnes); //CrŽation d'un model de tableau non Žditable avec les donnŽes dedans.
			tableau = new JTable();
			tableau.addMouseListener(this);
			tableau.setModel(this.tab); // Ajout du mod�le non Žditable ˆ la JTable
			this.scrollpane.setVisible(false);
			this.scrollpane = new JScrollPane(tableau);
			getContentPane().add(this.scrollpane) ;
			this.pane.add(this.scrollpane);
			this.scrollpane.setBounds(30,180,380,290);

		}catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	@SuppressWarnings("static-access")
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == this.okBut){
			this.connec();
		}
		//romeo | c'est partie est a modifier, je pense qu'il faudra revoir l'ergonomie de l'appel à PDF
		else if(e.getSource() == this.PDFBut){
			//PDF p = new PDF(client,données[][]);
		}
		//
		else if(e.getSource() == this.miRechercherM){
			JOptionPane.showMessageDialog(this, "Vous �tes dŽjˆ sur le formulaire de recherche de mod�le !", "Attention",1,new ImageIcon("./images/icon/warn.png"));
		}
		else if(e.getSource() == this.miAjouterM){
			//this.setVisible(false);
			this.dispose();
			@SuppressWarnings("unused")
			AjouterModele maFenetre = new AjouterModele();
		}
		else if(e.getSource() == this.miAjouterC){
			//this.setVisible(false);
			this.dispose();
			@SuppressWarnings("unused")
			AjouterClient maFenetre = new AjouterClient();
		}
		else if(e.getSource() == this.visBut){

			this.setDesiQ(this.getDonnees() [this.ligneVisu][1]);
			this.setNumModeleQ(this.getDonnees() [this.ligneVisu][2]);
			this.setNumPlanQ(this.getDonnees() [this.ligneVisu][3]);
			String testQ = new String("");
			testQ = "SELECT * FROM modele where designation_modele = \"" + desiQ + "\" and numero_modele = \"" + numModeleQ + "\" and numero_plan_modele = \"" + numPlanQ + "\" ;";
			//System.out.println(testQ);
			String id = new String("");
			SelectData.REQUETE = testQ;
			try{
				ResultSet result2 = SelectData.startSelect();
				try{
					while(result2.next()){
						id = result2.getString("id_modele");
					}

				}catch(SQLException ex){
					System.out.println(""); 
				}
			}catch (Exception e1) {
				e1.printStackTrace();
			}
			JOptionPane.showMessageDialog(this,id);
		}
		else if(e.getSource() == this.deconnexion){
			//this.setVisible(false);
			this.dispose();
			@SuppressWarnings("unused")
			Connexion c = new Connexion();	
		}
	}

	


}